package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.ErrorCodes;
import unipi.database.Rights;
import unipi.helpers.HTMLPages;
import unipi.helpers.LoginCookie;

public class GiveRights extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
	
	public GiveRights()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}
	
	private String getPage(String uid, boolean showTask, boolean showUser, boolean showDpt, String message)
	{	
				
		List<List<String>> userList = Rights.getUsers(uid, Rights.RightCols.UserRights);
				
		String content = "<form action=\"GiveRights\" method=\"post\">";
		
		
		content += "<h1>Ανάθεση Δικαιωμάτων</h1>" +
						  "<div class=\"ddldivstyle\">" + 
						  HTMLPages.getDropDownList(userList, "users", "none", "--Επιλέξτε Χρήστη--") + "</div><br />";
				
		if(showTask)
		{
			List<List<String>> users = Rights.getUsers(uid, Rights.RightCols.TaskRights);
			List<List<String>> dpts = Rights.getDpts(uid, Rights.RightCols.TaskRights);
			
			content += " <h2>Δικαίωματα Εργασιών</h2>";
			
			content += "<h3>ανα Τμήμα</h3>";
			content += "<div class=\"ddldivstyle\">"+
					HTMLPages.getDropDownList(dpts, "taskRightDpt", "none", "--Επιλέξτε Τμήμα--") + "</div><br />";	
			
			
			content += "<h3>ανά Χρήστη</h3>";			
			content += "<div class=\"ddldivstyle\">"+
					HTMLPages.getDropDownList(users, "taskRightUser", "none", "--Επιλέξτε Χρήστη--") + "</div><br />";	
			
			
		}
		
		if(showUser)
		{
			content += "<h2>Δικαίωματα Χρήστη</h2>";
			
			List<List<String>> dpts = Rights.getDpts(uid, Rights.RightCols.UserRights);
			
			content += "<div class=\"ddldivstyle\">"+
						HTMLPages.getDropDownList(dpts, "userRightDpt", "none", "--Επιλέξτε Τμήμα--") + "</div><br />";			
		}
		
		if(showDpt)
		{
			content += "<h2>Δικαίωματα Τμημάτων</h2>";
			
			List<List<String>> dpts = Rights.getDpts(uid, Rights.RightCols.DptRights);
			
			content += "<div class=\"ddldivstyle\">"+
						HTMLPages.getDropDownList(dpts, "dptRightDpt", "none", "--Επιλέξτε Τμήμα--") + "</div><br />";			
		}	

		content += "<br /><input class=\"longbutton\" type=\"submit\" value=\"Αναθεση\" />";
		content += "</form>" + message;
		
		return HTMLPages.getGeneralPage(content, uid);	
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
		
			request.setCharacterEncoding("utf-8");
			String userID = request.getParameter("users");
			String taskRightDpt = request.getParameter("taskRightDpt");
			String taskRightUser = request.getParameter("taskRightUser");
			String userRightDpt = request.getParameter("userRightDpt");
			String dptRightDpt = request.getParameter("dptRightDpt");
			
			String message = "";
			
			if(!userID.equals("none"))
			{
				//set up taskRights	
				if(taskRightDpt != null)
					if(!taskRightDpt.equals("none"))
					{
						ErrorCodes e = Rights.giveDptRights(userID, taskRightDpt, Rights.RightCols.TaskRights);
						if(e != ErrorCodes.OK)
							message += "Παρουσιάστηκε πρόβλημα με την ανάθεση δικαιωμάτων εργασιών ανά τμήμα.<br />";
					}
				
				if(taskRightUser != null)
					if(!taskRightUser.equals("none"))
					{
						if(!taskRightUser.equals(userID))
						{
							ErrorCodes e = Rights.giveUserRights(userID, taskRightUser);
							if(e == ErrorCodes.duplicateUsernames)
								message += "Τα επιλεγμένα δικαιώματα έχουν ήδη δωθεί.<br />";
							else if(e != ErrorCodes.OK)
								message += "Παρουσιάστηκε πρόβλημα με την ανάθεση δικαιωμάτων εργασιών ανά χρήστη.<br />";
						}
					}				
				
				//set up userRights
				if(userRightDpt != null)
					if(!userRightDpt.equals("none"))
					{
						ErrorCodes e = Rights.giveDptRights(userID, userRightDpt, Rights.RightCols.UserRights);
						if(e != ErrorCodes.OK)
							message += "Παρουσιάστηκε πρόβλημα με την ανάθεση δικαιωμάτων χρηστών. <br />";
					}					
				
				//set up dptRights
				if(dptRightDpt != null)
					if(!dptRightDpt.equals("none"))
					{
						ErrorCodes e = Rights.giveDptRights(userID, dptRightDpt, Rights.RightCols.DptRights);
						if(e != ErrorCodes.OK)
							message += "Παρουσιάστηκε πρόβλημα με την ανάθεση δικαιωμάτων τμημάτων. <br />";
					}	
				
			}
			
			if(message.equals("")) message = "Τα επιλεγμένα δικαιώματα καταχωρήθηκαν με επιτυχία.";
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			Map<String, Boolean> rights = Rights.getRights(uid);
			
			out.println(getPage(uid, rights.get("taskRights"), rights.get("userRights"), rights.get("dptRights"), message));
			
			out.close();
			
		}
	
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			Map<String, Boolean> rights = Rights.getRights(uid);
			
			out.println(getPage(uid, rights.get("taskRights"), rights.get("userRights"), rights.get("dptRights"), ""));
			
			out.close();			
		}
	}

}
